import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class OdbcMenuItem extends MenuItem {
	
	private String currentStatus;
	private connectionObj co;
	private boolean firstRefreshDone = false;
	
	public OdbcMenuItem (connectionObj co) {
		this.co = co;
		currentStatus = dbRetriever.checkConnection(co.getConnectionString());
		if (currentStatus.equals("true")) { 
			this.setLabel(co.getConnectionName() + " -- OK");
		} else { 
			this.setLabel(co.getConnectionName() + " -- Failed:" + currentStatus);
		}
		this.addActionListener(new ClickListener());
	}
	
	public String refreshStatus () { 
		String newStatus = dbRetriever.checkConnection(co.getConnectionString());
		if (newStatus.equals("true")){
			this.setLabel(co.getConnectionName() + " -- OK");
		} else { 
			this.setLabel(co.getConnectionName()  + " -- Failed:" + newStatus);
		}
		if (firstRefreshDone==false) { 
			currentStatus = newStatus;
			if (currentStatus.equalsIgnoreCase("true")) { 
				co.runOkActions();
			} else { 
				co.runFailActions();
			}
			firstRefreshDone = true;
			return this.getLabel();
		}
		if (newStatus != currentStatus) { 
			currentStatus = newStatus;
			if (currentStatus.equalsIgnoreCase("true")) { 
				co.runOkActions();
			} else { 
				co.runFailActions();
			}			
			return this.getLabel();
		} else { 
			return null;
		}
		
	}
	
	private class ClickListener implements ActionListener {
				
		public ClickListener() { 

		}
		
		public void actionPerformed(ActionEvent e) {
				co.runClickActions();
		}
	}
}